import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    // 重定向 -- 嵌套路由时考虑页面配置完之后是否会发生白屏
    redirect:'/home/index'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    
    children:[
      {
        path:'index',
        name:'/home/index',
        component:()=>import ('../views/Index.vue'),
        meta:{nav:['首页']},
      },
      {
        path:'actor-list',
        name:'/home/actor-list',
        component:()=>import ('../views/actor/ActorList.vue'),
        meta:{ //动态生成导航
          nav:['演员管理','演员列表']
        }
      },
      {
        path:'actor-add',
        name:'/home/actor-add',
        component:()=>import ('../views/actor/ActorAdd.vue'),
        meta:{
          nav:['演员管理','新增演员']
        }
      },
      {
        path:'director-list',
        name:'/home/director-list',
        component:()=>import ('../views/director/DirectorList.vue'),
        meta:{
          nav:['导演管理','导演列表']
        }
      },
      {
        path:'director-add',
        name:'/home/director-add',
        component:()=>import ('../views/director/DirectorAdd.vue'),
        meta:{
          nav:['导演管理','新增导演']
        }
      },
      {
        path:'movie-add',
        name:'/home/movie-add',
        component:()=>import ('../views/movie/MovieAdd.vue'),
        meta:{
          nav:['电影管理','新增电影']
        }
      },
      {
        path:'movie-update/:id',
        name:'/home/movie-update',
        component:()=>import ('../views/movie/MovieUpdate.vue'),
        meta:{
          nav:['电影管理','修改电影']
        }
      },
      {
        path:'movie-list',
        name:'/home/movie-list',
        component:()=>import ('../views/movie/MovieList.vue'),
        meta:{
          nav:['电影管理','电影列表']
        }
      },
      {
        path:'cinema-list',
        name:'/home/cinema-list',
        component:()=>import ('../views/cinema/CinemaList.vue'),
        meta:{
          nav:['电影院管理','电影院列表']
        }
      },
      {
        path:'cinema-add',
        name:'/home/cinema-add',
        component:()=>import ('../views/cinema/CinemaAdd.vue'),
        meta:{
          nav:['电影院管理','新增电影院']
        }
      },
      {
        path:'cinema-room-list/:cinema_id',
        name:'/home/cinema-room-list',
        component:()=>import ('../views/cinema/CinemaRoomList.vue'),
        meta:{
          nav:['电影院管理','放映厅列表']
        }
      },
      // 路径需要接收上一个页面参数的是在接收参数的页面配置路由时在路径后面拼接参数 
      {
        path:'showingonplan-add/:room_id',
        name:'/home/showingonplan-add',
        component:()=>import ('../views/cinema/showingonPlanAdd.vue'),
        meta:{
          nav:['电影院管理','电影院列表','放映厅列表','新增排片计划']
        }
      },
      {
        path:'showingonplan-list/:room_id',
        name:'/home/showingonplan-list',
        component:()=>import ('../views/cinema/showingonPlanList.vue'),
        meta:{
          nav:['电影院管理','电影院列表','放映厅列表','新增排片计划']
        }
      },
      {
        path:'seat-template/:room_id',
        name:'/home/seat-template',
        component:()=>import ('../views/cinema/CinemaRoomSeatTemplate.vue'),
        meta:{
          nav:['电影院管理','电影院列表','放映厅列表','新增排片计划']
        }
      },
    ]
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('../views/user/Login.vue')
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局的前置导航守卫
import store from '../store'
router.beforeEach((to,from,next)=>{
  console.log('from',from);
  console.log('to',to);
  
  if(to.path=='/login'){ // 如果目的地址是->登录页面就可以继续跳转
    next()
  }else{                 //否则就要读取用户是否登录
    
    if(store.state.user){
      next()// 登录了就可以继续跳转
    }else{ 
      // 没有登录就会跳转到登录页
      router.push('/login')
    }
  }
  
})

export default router
